﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Practices.Prism.Commands;

using VideoPlay.Adapters;
using VideoPlay.Services;
using VideoPlay.Services.Models;
using VideoPlay.PhoneClient.ViewModels;

namespace VideoPlay.PhoneClient.ViewModels
{
    public class SearchVideoViewModel : ViewModel
    {
        private int currentPage = 1;

        private ObservableCollection<VideoViewModel> videos;
        public ObservableCollection<VideoViewModel> Videos
        {
            get 
            {
                if (this.videos == null)
                    this.videos = new ObservableCollection<VideoViewModel>();
                return this.videos;
            }
            set
            {
                this.videos = value;
                this.RaisePropertyChanged(()=>this.Videos);
            }
        }

        private string searchTerm;
        public string SearchTerm
        {
            get 
            {
                if (this.searchTerm == null)
                    this.searchTerm = string.Empty;
                return searchTerm; 
            }
            set
            {
                this.searchTerm = value;
                this.RaisePropertyChanged(()=>this.SearchTerm);
            }
        }

        private bool isLoadMore = false;
        public bool IsLoadMore
        {
            get { return this.isLoadMore; }
            set
            {
                this.isLoadMore = value;
                this.RaisePropertyChanged(() => this.IsLoadMore);
            }
        }

        public Visibility LoadMoreButtonVisibility
        {
            get
            {
                return Videos.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public DelegateCommand SearchCommand { get; set; }

        public DelegateCommand SearchMoreCommand { get; set; }

        public SearchVideoViewModel(
            INavigationService navigationService,
            IPhoneService phoneService,
            IndicatorViewModel indicator)
            : base(navigationService, phoneService, indicator, new Uri("Views/SearchVideoView.xaml", UriKind.Relative))
        {
            SearchCommand = new DelegateCommand(this.SearchVideos);
            SearchMoreCommand = new DelegateCommand(this.SearchMore);

        }

        private void SearchVideos()
        {
            Indicator.Show();

            this.Videos = new ObservableCollection<VideoViewModel>();
            this.PhoneServiceFacade.SearchVideosV2(SearchTerm, 0, 10, OnSearchCompleted);
        }

        private void SearchMore()
        {
            //Indicator.Show();
            IsLoadMore = true;

            currentPage++;
            this.PhoneServiceFacade.SearchVideosV2(SearchTerm, currentPage, 10, OnSearchCompleted);
        }

        private void OnSearchCompleted(object param)
        {
            List<List> videos = param as List<List>;
            if (videos == null)
            {
                Indicator.Hide();
                MessageBox.Show("Ops!!! Cannot connect to Youtube data service.");
                return;
            }

            foreach (var video in videos)
            {
                VideoViewModel vm = new VideoViewModel(NavigationService,PhoneServiceFacade,Indicator);
                vm.ImageUrl = video.image;
                vm.VideoName = video.title;
                vm.Duration = video.duration.ToString();
                vm.Like = IsFavoriteVideo(video.id, video.youtubeId);
                vm.VideoUrl = video.url;
                vm.YoutubeId = video.youtubeId;

                this.Videos.Add(vm);
            }
            this.RaisePropertyChanged(()=>this.LoadMoreButtonVisibility);
            Indicator.Hide();
            IsLoadMore = false;
        }

        private bool IsFavoriteVideo(int id,string youtubeId)
        {
            List video = FavoriteFacade.GetVideoById(id,youtubeId);
            return video == null ? false : true;
        }

        public override void OnPageResumeFromTombstoning()
        {
            
        }
    }
}
